<% @page_title = "#{@project.name} - #{@tag.name}"

members = @current_members = {}
@tag.member_ids.each do
  next if @tag_type == "subject" && UBID.uuid_class_match?(it, ApiKey)
  members[it] = nil
end
UBID.resolve_map(members)

editable =
  !(@tag_type == "subject" && @tag.name == "Admin") &&
    has_project_permission("Project:#{@tag_type.sub(/(ect|ion)\z/, "").downcase}tag")

addition_allowed = has_permission?("#{@tag_model}:add", @authorize_id)
removal_allowed = has_permission?("#{@tag_model}:remove", @authorize_id)

add_groups = @tag_model.options_for_project(@project)
avoid = @current_members.dup
@tag.currently_included_in.each { avoid[it] = true }
avoid[@tag.id] = true
add_groups.transform_values! do |v|
  v = v.reject { avoid.include?(it.id) }
  v.map! { [it.name.to_s, it.ubid] }
  v.sort!
end
add_groups.reject! { _2.empty? }

add_checked = typecast_body_params.array(:nonempty_str, "add")
remove_checked = typecast_body_params.array(:nonempty_str, "remove")

base_path = path(@tag)
associate_path = "#{base_path}/associate"
disassociate_path = "#{base_path}/disassociate"
current_members =
  if @tag_type == "object"
    @current_members.map { [object_tag_membership_label(_2), _2.ubid] }
  else
    @current_members.map { [ace_label(_2), _2.ubid] }
  end
current_members.sort! %>
<%== render("project/user-tabbar") %>

<div class="space-y-1">
  <%== part(
    "components/page_header",
    title: "#{@display_tag_type} Tag: #{@tag.name}",
    breadcrumbs: [
      %w[Projects /project],
      [@project.name, @project.path],
      ["Access Control", "#{@project.path}/user/access-control"],
      ["#{@display_tag_type} Tags", "#{@project.path}/user/access-control/tag/#{@tag_type}"],
      [@tag.name, "#"]
    ]
  ) %>
</div>

<div class="grid gap-6">
  <% if editable %>
    <div class="overflow-hidden rounded-lg shadow ring-1 ring-black ring-opacity-5 bg-white divide-y divide-gray-200">
      <div class="px-4 py-5 sm:p-6 space-y-2">
        <h3 class="text-lg font-medium leading-6 text-gray-900"><%= "Update #{@display_tag_type} Tag" %></h3>
        <% form(action: path(@tag), method: :post) do %>
          <div class="grid grid-cols-12 gap-3">
            <div class="col-span-12 md:col-span-5">
              <%== part(
                "components/form/text",
                name: "name",
                button_title: "Update",
                label: "Name",
                value: @tag.name,
                attributes: {
                  required: true,
                  placeholder: "Name"
                }
              ) %>
            </div>
          </div>
        <% end %>
      </div>
    </div>
  <% end %>
  <div>
    <% if current_members.empty? %>
      <div class="md:flex md:items-center md:justify-between pb-1 lg:pb-2">
        No current members of
        <%= @tag_type %>
        tag.
      </div>
    <% else %>
      <div class="md:flex md:items-center md:justify-between pb-1 lg:pb-2">
        <div class="min-w-0 flex-1">
          <h3 class="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-2xl sm:tracking-tight">Current Members</h3>
        </div>
      </div>
      <% form(action: disassociate_path, method: :post) do %>
        <div class="overflow-hidden rounded-lg shadow ring-1 ring-black ring-opacity-5 bg-white divide-y divide-gray-200">
          <table id="tag-membership-remove" class="min-w-full divide-y divide-gray-300">
            <thead class="bg-gray-50">
              <tr>
                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Name</th>
                <% if removal_allowed %>
                  <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Remove?</th>
                <% end %>
              </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 bg-white">
              <% current_members.each do |label, ubid| %>
                <tr id="<%= ubid %>" class="cursor-pointer">
                  <td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6" scope="row">
                    <%= label %>
                  </td>
                  <% if removal_allowed %>
                    <td class="py-4 pl-3 pr-4 text-right sm:pr-6">
                      <%== part(
                        "components/form/checkbox",
                        name: "remove[]",
                        options: [[ubid, nil, nil, ({ checked: "checked" } if remove_checked&.include?(UBID.to_uuid(ubid)))]]
                      ) %>
                    </td>
                  <% end %>
                </tr>
              <% end %>
            </tbody>
          </table>
        </div>
        <% if removal_allowed %>
          <div class="py-4">
            <%== part("components/form/submit_button", text: "Remove Members") %>
          </div>
        <% end %>
      <% end %>
    <% end %>
  </div>
  <div>
    <% if addition_allowed && !add_groups.all? { _2.empty? } %>
      <div class="md:flex md:items-center md:justify-between pb-1 lg:pb-2">
        <div class="min-w-0 flex-1">
          <h3 class="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-2xl sm:tracking-tight">Add Members</h3>
        </div>
      </div>
      <% form(action: associate_path, method: :post) do %>
        <div class="overflow-hidden rounded-lg shadow ring-1 ring-black ring-opacity-5 bg-white divide-y divide-gray-200">
          <table id="tag-membership-add" class="min-w-full divide-y divide-gray-300">
            <thead class="bg-gray-50">
              <tr>
                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Name</th>
                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Add?</th>
              </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 bg-white">
              <% add_groups.each do |name, available_to_add| %>
                <% name = name["label"] if name.is_a?(Hash) %>
                <tr>
                  <th colspan="2" class="text-left px-3 py-3.5 bg-gray-50"><%= name %></th>
                </tr>
                <% available_to_add.each do |label, ubid| %>
                  <tr id="<%= ubid %>" class="cursor-pointer">
                    <td class="whitespace-nowrap py-4 pl-5 pr-3 text-sm font-medium text-gray-900 sm:pl-7" scope="row">
                      <%= label %>
                    </td>
                    <td class="py-4 pl-3 pr-4 text-right sm:pr-6">
                      <%== part(
                        "components/form/checkbox",
                        name: "add[]",
                        options: [[ubid, nil, nil, ({ checked: "checked" } if add_checked&.include?(ubid))]]
                      ) %>
                    </td>
                  </tr>
                <% end %>
              <% end %>
            </tbody>
          </table>
        </div>
        <div class="py-4">
          <%== part("components/form/submit_button", text: "Add Members") %>
        </div>
      <% end %>
    <% end %>
  </div>
</div>
